﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.IO;
using System.Data;

namespace wubi86
{
    public class JsonUtils {

        public static DataTable DeserializeObject(string value) {
            return (JsonUtils.DeserializeObject(value, typeof(DataTable)) as DataTable);
        }

        public static Dictionary<string, string> DeserializeDictionaryObject(string value){
            return (JsonUtils.DeserializeObject(value, typeof(Dictionary<string,string>)) as Dictionary<string, string>);
        }

        public static object DeserializeObject(string value, Type type) {
            // Get the embedded resource stream
            Stream dllStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("wubi86.Newtonsoft.Json.dll");

            // Read the stream into a byte array
            byte[] dllBytes = new byte[dllStream.Length];
            dllStream.Read(dllBytes, 0, dllBytes.Length);

            // Load the byte array into an Assembly (this represents the DLL)
            Assembly dllAssembly = Assembly.Load(dllBytes);

            List<Type> lstType = dllAssembly.GetTypes().ToList();
            List<Type> lstType2 = new List<Type>() { };
            foreach (Type t in lstType) {
                if (!t.IsAbstract && !t.IsInterface) {
                    if (t.FullName.ToLower().Contains("convert"))
                        lstType2.Add(t);
                }
            }

            // Now you can use the Assembly to get types and create instances, etc.
            Type myType = dllAssembly.GetType("Newtonsoft.Json.JsonConvert");
            
            
             MethodInfo methodInfo = myType.GetMethods()[40];

            // If the method has parameters, you need to create an object array with the parameters you want to pass
            //object[] parameters = new object[] { "param1", "param2" }; // Replace with actual parameters

            // Now you can call the method
            object[] parameters = new object[] { value, type };
            object result = methodInfo.Invoke(null, parameters);

            // If the method has a return value, it's in the result variable
            //Console.WriteLine(result);
            return result;
        }

        public static string SerializeObject(object o) {
            // Get the embedded resource stream
            Stream dllStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("wubi86.Newtonsoft.Json.dll");

            // Read the stream into a byte array
            byte[] dllBytes = new byte[dllStream.Length];
            dllStream.Read(dllBytes, 0, dllBytes.Length);

            // Load the byte array into an Assembly (this represents the DLL)
            Assembly dllAssembly = Assembly.Load(dllBytes);

            List<Type> lstType = dllAssembly.GetTypes().ToList();
            List<Type> lstType2 = new List<Type>() { };
            foreach (Type t in lstType) {
                if (!t.IsAbstract && !t.IsInterface) {
                    if (t.FullName.ToLower().Contains("convert"))
                        lstType2.Add(t);
                }
            }

            // Now you can use the Assembly to get types and create instances, etc.
            Type myType = dllAssembly.GetType("Newtonsoft.Json.JsonConvert");


            MethodInfo methodInfo = myType.GetMethods()[27];

            // If the method has parameters, you need to create an object array with the parameters you want to pass
            //object[] parameters = new object[] { "param1", "param2" }; // Replace with actual parameters

            // Now you can call the method
            object result = methodInfo.Invoke(null, new object[] { o });

            // If the method has a return value, it's in the result variable
            //Console.WriteLine(result);
            return result != null ? result.ToString() : "";
        }
    }
}
